//
//  UIImageView+navinfo.m
//  gweAD
//
//  Created by navinfo air on 2016/12/26.
//  Copyright © 2016年 mengy. All rights reserved.
//

#import "UIImageView+Navinfo.h"
#import "AFAutoPurgingImageCache.h"

@implementation UIImageView (Navinfo)


- (void)setImageURL:(NSString *)imageUrl  finish:(void (^)(NSError *error)) finishBlock{
    
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
//    if(openHttpsSSL)
//    {
//        [manager setSecurityPolicy:[self customSecurityPolicy]];
//    }
    imageUrl = [imageUrl stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    NSURL *URL = [NSURL URLWithString:imageUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        long long i = downloadProgress.completedUnitCount*100/downloadProgress.totalUnitCount;
        NSLog(@"进度%d",i);
    } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        NSURL *fileUrl = [documentsDirectoryURL URLByAppendingPathComponent:@"startAppGenralizeImage"];
        NSFileManager *manager = [NSFileManager defaultManager];
        if ([manager isDeletableFileAtPath:[fileUrl path]]) {
            NSError *error;
            [manager removeItemAtPath:[fileUrl path] error:&error];
            if(error){
                return fileUrl;
            }
        }
        return fileUrl;
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        if (!error) {
            NSString *imgPath = [filePath.absoluteString stringByReplacingOccurrencesOfString:@"file://" withString:@""];
            UIImage *image = [UIImage imageWithContentsOfFile:imgPath];
            [self setImage:image];
            self.contentMode = UIViewContentModeScaleAspectFit;
            if (image) {
                finishBlock(nil);
            }
            else{
                NSError *error = [[NSError alloc]init];
                finishBlock(error);
            }
            
        }
        else
        {
            
            NSError *error = [[NSError alloc]init];
            finishBlock(error);
        }
    }];
    [downloadTask resume];
}

/**
- (AFSecurityPolicy*)customSecurityPolicy
{
    // AFSSLPinningModeCertificate 使用证书验证模式
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    
    // allowInvalidCertificates 是否允许无效证书（也就是自建的证书），默认为NO
    // 如果是需要验证自建证书，需要设置为YES
    securityPolicy.allowInvalidCertificates = YES;
    
    //validatesDomainName 是否需要验证域名，默认为YES；
    //假如证书的域名与你请求的域名不一致，需把该项设置为NO；如设成NO的话，即服务器使用其他可信任机构颁发的证书，也可以建立连接，这个非常危险，建议打开。
    //置为NO，主要用于这种情况：客户端请求的是子域名，而证书上的是另外一个域名。因为SSL证书上的域名是独立的，假如证书上注册的域名是www.google.com，那么mail.google.com是无法验证通过的；当然，有钱可以注册通配符的域名*.google.com，但这个还是比较贵的。
    //如置为NO，建议自己添加对应域名的校验逻辑。
    securityPolicy.validatesDomainName = NO;
    
    //先导入证书
    NSString *cerStr = [[NSUserDefaults standardUserDefaults] objectForKey:SSL_CER];
    if (cerStr) {
        NSData *dataFromBase64String = [[NSData alloc]
                                        initWithBase64EncodedString:cerStr options:0];
        securityPolicy.pinnedCertificates = [[NSSet alloc] initWithObjects:dataFromBase64String, nil];
        //@[dataFromBase64String];
    }
    
    return securityPolicy;
}
*/

@end
